Skip to content

Conversation

mrMetalWood
Copy link
Collaborator

@mrMetalWood mrMetalWood commented Oct 7, 2025

This fixes a few issues with the AdvancedMarker when using anchor points. Issues where introduced when Google Maps started to use the gmp-advanced-marker element by default for the marker.

I tried multiple approaches to achieve a "clean" solution, but none of them worked 100%.

Changes:

  • Add a data-origin="rgm" attribute to the gmp-advanced-marker to be able to overwrite the pointer events with a global style tag and to only target the markers originating from this library
  • Moving the anchor points translation to the first child of the marker for the collision detection to work correctly
  • Introducing a global style helper to be able to inject a style tag with custom styles
  • Also made some adjustments to the example

Explanations

Collision detection:

  • Google Maps already correctly used the marker content for collision detection. But it looks at the position of the first child. I now moved our translates to the first child instead of a deeper nested element like before.
  • The cleanest solution would be to translate the whole marker, but that breaks collision detection. Apparently the Google Maps API determines the collision detection by the difference between marker container and content. When there is no difference collision detection is based on original bottom/center anchor point

Pointer events:

  • Issue: As soon as there is an onClick event listener for the marker, the marker container gets all pointer events. That means when content is translated by an anchor point, mouse events are also triggered when hovering over the "ghost" element.
  • I found no other solution than to overwrite the pointer events for the gmp-advanced-marker

This is what I found to be the least intrusive solution.

fixes #819

@mrMetalWood mrMetalWood marked this pull request as ready for review October 17, 2025 10:07
Adds support for the 'anchorLeft' and 'anchorTop' properties on the AdvancedMarker component. This provides a more direct way to control the anchor point of the marker, especially with modern versions of the Google Maps JavaScript API.

The implementation includes:
- A new internal 'useAdvancedMarkerAnchorPoint' hook to encapsulate anchoring logic.
- Version detection to use native 'anchorLeft'/'anchorTop' properties on Google Maps API v3.62+ and fallback to a CSS transform on older versions.
- A warning is logged when using the new props on unsupported API versions.
- Added TypeScript definitions of anchor options to type augmentation
- Added API documentation.
Adds a suite of tests for the anchor-related props ('anchorLeft', 'anchorTop', and 'anchorPoint') on the AdvancedMarker component.

This suite covers:
- Precedence of 'anchorLeft'/'anchorTop' over 'anchorPoint' on modern APIs.
- Correct fallback to 'anchorPoint' on modern APIs.
- Correct application of 'anchorPoint' via CSS transform on legacy APIs.
- Warning generation when using modern props on legacy APIs.
- Snapshot testing for console warnings.
Marks the 'anchorPoint' prop as deprecated in favor of the 'anchorLeft' and 'anchorTop' props.

- Adds a '@deprecated' JSDoc tag to the 'anchorPoint' prop.
- Adds a TODO comment to add a console warning in a future version.
@usefulthink usefulthink merged commit d7b128e into visgl:main Oct 21, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] anchorPoint creates duplicated clickable area

2 participants